var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    // text:"这是一个页面"
    navList: [],
    goodsList: [],
    count:0,
    id: '',
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    size: 10,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1,
    // 导航
    imageurl1: "",
    daindex1: 0,
    imageurl2: "",
    daindex2: 0,
    keyword: '',
    topNum: 0,
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    console.log(options.keyword);

    if (options.goods_typeID) {
      that.setData({
        id: parseInt(options.goods_typeID)
      });
    }
    if (options.keyword) {
      that.setData({
        keyword: options.keyword,
      });
    }
    this.getGoodsList();

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });


    //this.getCategoryInfo();

  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
    console.log(1);
  },
  onHide: function() {
    // 页面隐藏
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getGoodsList()
  },

  onPullDownRefresh() {
    // 增加下拉刷新数据的功能
    var self = this;
    this.getGoodsList();

    wx.stopPullDownRefresh();
  },

  getGoodsList: function() {
    var that = this;

    if (that.data.totalPages <= that.data.page - 1) {
      that.setData({
        nomore: true
      })
      return;
    }

    wx.request({
      url: api.getGoodsList,
      data: {
        goods_typeID: that.data.id,
        page: that.data.page,
        keyword: that.data.keyword,
      },
      method: 'GET',
      success: function(res) {
        console.log(res);
        if (res.data.status === 1) {
          that.setData({
            goodsList: that.data.goodsList.concat(res.data.result),
            count: res.data.count,
            page: that.data.page + 1,
            totalPages: res.data.totalPages,
          })
        }
      }
    });
  },
  onUnload: function() {
    // 页面关闭
  },
  // switchCate: function (event) {
  //   if (this.data.id == event.currentTarget.dataset.id) {
  //     return false;
  //   }
  //   var that = this;
  //   var clientX = event.detail.x;
  //   var currentTarget = event.currentTarget;
  //   if (clientX < 60) {
  //     that.setData({
  //       scrollLeft: currentTarget.offsetLeft - 60
  //     });
  //   } else if (clientX > 330) {
  //     that.setData({
  //       scrollLeft: currentTarget.offsetLeft
  //     });
  //   }
  //   this.setData({
  //     id: event.currentTarget.dataset.id,
  //     page:1,
  //     totalPages: 1,
  //     goodsList: [],
  //     nomore: false
  //   });

  //   this.getCategoryInfo();
  // }


  // 导航栏点击事件
  choosesort1: function(e) {
    if (this.data.daindex1 == 0) {
      this.setData({
        imageurl1: "",
        daindex1: 1
      })
    } else {
      this.setData({
        imageurl1: "",
        daindex1: 0
      })
    }

  },
  choosesort2: function(e) {
    if (this.data.daindex2 == 0) {
      this.setData({
        imageurl2: "",
        daindex2: 1
      })
    } else {
      this.setData({
        imageurl2: "",
        daindex2: 0
      })
    }
  },

  allCategory: function(e) {
    wx.navigateTo({
      url: '/pages/category/category?id='+'',
    })
  },
  bindInputKeyword: function(e) {
    this.setData({
      keyword: e.detail.value,
    })
  },
  search: function() {
    var that = this;
    wx.showToast({
      title: '搜索中',
      icon: 'loading',
    });
    wx.navigateTo({
      url: '/pages/category/category?keyword=' + that.data.keyword,
    })
  },
  // 获取滚动条当前位置
  scrolltoupper: function (e) {
    console.log(e)
    if (e.detail.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },
})